package com.fpt.group3.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.fpt.group3.service.exception.SystemException;

/**
 * @author John Tran
 */
public class ConnectDB {
    /**
     * reference connection object.
     */
    private static Connection con = null;
    /**
     * url of data connection.
     */
    private String url;
    /**
     * jdbc driver.
     */
    private String driver;
    /**
     * user name of server.
     */
    private String username;
    /**
     * password.
     */
    private String password;

    /**
     * open connection.
     * @return connection opened
     * @throws SystemException throws exception
     */
    public final Connection open() throws SystemException {
        try {
            Class.forName(driver).newInstance();
            con = DriverManager.getConnection(url, username, password);
            return (con);
        } catch (ClassNotFoundException e) {
            throw new SystemException(new Object() { }
            .getClass(), this.getClass().getSimpleName(), e.getMessage());
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            throw new SystemException(new Object() { }
            .getClass(), this.getClass().getSimpleName(), e.getMessage());

<<<<<<< .mine
        } finally {
            Logger.getLogger(ConnectDB.class.getName()).info(
                    "Established connection to " + url);
        }
        return null;
=======
        }
        return null;
>>>>>>> .r18

    }

    /**
     * close connection.
     * @throws SystemException throws system exception
     */
    public final void close() throws SystemException {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                throw new SystemException(new Object() { }
                .getClass(), this.getClass().getSimpleName(), e.getMessage());
            }
        }
    }

    /**
     * get connection info.
     * @throws SystemException throws exception
     */
    public ConnectDB() throws SystemException {
        Properties properties = new Properties();
        try {
            properties.load(this.getClass().getClassLoader()
                    .getResourceAsStream("dbconnection.properties"));
            url = properties.getProperty("jdbc.url");
            driver = properties.getProperty("jdbc.driver");
            username = properties.getProperty("jdbc.username");
            password = properties.getProperty("jdbc.password");
        } catch (IOException e) {
            Logger.getLogger(ConnectDB.class.getName()).error(
                    "dbconnection.properties is missing!");
            throw new SystemException(new Object() { }
            .getClass(), this.getClass().getSimpleName(), e.getMessage());
        }
    }
}
